<template>
  <div class="add-category-con">
    <a-modal :visible="visible" :on-before-ok="handleOk" @before-open="open" @cancel="handleCancel" title-align="start" :mask-closable="false">
      <template #title> {{ title }} </template>
      <div>
        <a-form ref="formRef" size="mini" :model="form">
          <a-form-item label-col-flex="true" :label-col-props="{ span: 5 }" field="name" label="品牌名称" :rules="rules.name">
            <a-input v-model="form.name" placeholder="请输入" />
          </a-form-item>
          <a-form-item label-col-flex="true" :label-col-props="{ span: 5 }" field="name" label="品牌LOGO">
            <Uplaod :limit="1" v-model="form.logoPic" />
          </a-form-item>
          <a-form-item label-col-flex="true" :label-col-props="{ span: 5 }" field="name" label="描述">
            <a-input v-model="form.description" placeholder="请输入" />
          </a-form-item>
          <a-form-item label-col-flex="true" :label-col-props="{ span: 5 }" field="name" label="状态">
            <a-switch v-model="form.status" />
          </a-form-item>
        </a-form>
      </div>
    </a-modal>
  </div>
</template>

<script lang="ts">
  import { defineComponent, ref, computed, reactive } from 'vue'
  import Uplaod from '@/components/uploadImg.vue'
  import { addPropsType } from '../type'
  import { saveBrandData } from '@/api/product'
  import { Message } from '@arco-design/web-vue'
  import _ from 'lodash-es'
  export default defineComponent({
    name: 'addCateGory',
    emits: ['save'],
    props: ['detail'],
    components: {
      Uplaod,
    },
    setup(props: addPropsType, { emit }) {
      let visible = ref(false)
      let form: any = ref({
        name: '',
        status: true,
        description: '',
        logoPic: [],
      })
      const formRef = ref()

      const rules = {
        name: [{ required: true, message: '请输入计量单位名称' }],
      }

      const handleOk = (done: any) => {
        formRef.value.validate(async (valid: any) => {
          if (!valid) {
            if (form.value.logoPic instanceof Array) {
              form.value.logoPic = form.value.logoPic[0]?.url || ''
            }

            try {
              const res: any = await saveBrandData(form.value)
              if (res.code == 'SUCCESS') {
                visible.value = false
                form.value = {}
                props.detail.type == 'add' ? Message.success('新增成功') : Message.success('编辑成功')
                emit('save')
              }
              done(true)
            } catch (error) {
              done(true)
            }
          } else {
            done(true)
          }
        })
      }
      const handleCancel = () => {
        formRef.value.resetFields()
        form.value = {}
        visible.value = false
      }

      const title = computed(() => {
        if (props.detail.type == 'add' && !props.detail.data) {
          return '新增品牌'
        } else {
          return '编辑品牌'
        }
      })

      const open = () => {
        if (props.detail.type == 'edit') {
          form.value = _.cloneDeep(props.detail.data)
        } else {
          form.value = {
            name: '',
            status: true,
            description: '',
            logoPic: [],
          }
        }
      }

      return { visible, handleOk, handleCancel, open, title, form, rules, formRef }
    },
  })
</script>

<style lang="less" scoped>
  .add-category-con {
  }
</style>
